package net.studou.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.studou.fetion.LiteFetion;
import net.studou.fetion.bean.ClientState;
import net.studou.vo.VUser;

public class Utils {

	public final static SimpleDateFormat ALL = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public final static SimpleDateFormat NORMAL_2_YEAR = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat NORMAL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat NORAML_NO_TIME = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 获取当时时间
	 * @return
	 */
	public static Date getChinaDateTime() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}
	/**
	 * 获取当前日期，时间为0点0分0钞
	 * @return
	 */
	public static Date getChinaDateTimeZero() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	
	/**
	 * 根据当前时间获取偏移后的时间
	 * @param fromDate 开始时间
	 * @param month    偏移几个月，向未来偏移用正数，向过去偏移用负数
	 * @return
	 */
	public static Date getDateByMonth(Date fromDate, int month){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(fromDate);
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month);
		return calendar.getTime();
	}
	
	/**
	 * 检查上传的文件是否是指定的格式
	 * @param file
	 * @return
	 */
	public static boolean getImageFormat(File file) {
		FileInputStream is = null;
		byte[] b = new byte[3];
		try {
			is = new FileInputStream(file);
			is.read(b, 0, b.length);
		} catch (Exception e) {
			return false;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					return false;
				}
			}
		}

		StringBuilder stringBuilder = new StringBuilder();
		if (b == null || b.length <= 0) {
			return false;
		}
		for (int i = 0; i < b.length; i++) {
			int v = b[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		if ("ffd8ff".equalsIgnoreCase(stringBuilder.toString())) {
			return true;
		} else if ("89504e".equalsIgnoreCase(stringBuilder.toString())) {
			return true;
		} else if ("474946".equalsIgnoreCase(stringBuilder.toString())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 发送短信
	 * @param client
	 * @param msg
	 */
	public static void  sendSMS(LiteFetion client, String msg){
		if(client != null && client.getClientState() == ClientState.ONLINE){
			client.sendSelfSMS(msg);
		}
	}
	
	public static VUser copySession(VUser oUser, VUser nUser, boolean cp){
//		nUser.setLoginTime(oUser.getLoginTime());
//		nUser.setIp(oUser.getIp());
//		nUser.setOpe(oUser.isOpe());
//		if(!cp){
//			nUser.setPopedoms(oUser.getPopedoms());
//		}
		return nUser;
	}
}
